layui.use(['layer','form','table','element','layedit','upload','laydate'],function () {
    var layer = layui.layer
        ,form = layui.form
        ,table =layui.table
        ,element = layui.element
        ,layedit = layui.layedit
        ,upload = layui.upload
        ,laydate = layui.laydate;

    //日期控件
    laydate.render({
        elem: '#endTime' //指定元素
        ,format:"yyyy-MM-dd"
        ,min:minDate()
    });


    //普通图片上传
    var uploadInst = upload.render({
        elem: '#update_cover_btn'
        ,url: '/admin/advert/uploadImg'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            //如果上传失败
            console.log(res);
            var flag = res.flag;
            var message = res.message;
            if(!flag){
                layer.msg(message);
            }else{
                //上传成功,则返回url
                layer.msg("封面上传成功!");
                $("#url").attr("value",res.data.url);
            }
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //表单验证
    form.verify({
        id:function (value) {
            if(value == ""){
                return "id不能为空";
            }
        },
        url:function (value) {
            if(value == ""){
                return "封面不能为空";
            }
        }
    });

        //提交表单
    form.on('submit(form)',function (data) {

        //校验类型和标签是否选择
        // var label_length = $("input[name=label]:checked").length;
        // var type_length = $("input[name=type]:checked").length;
        layer.msg('保存中...', { icon: 16, shade: [0.5, '#f5f5f5'], scrollbar: false, offset: '50%', time: 300000 });



        var paramData = {
            "url":$("#url").val(),
            "endTime":$("#endTime").val(),
            "used":$("#used").val()
        };

        var data2 = JSON.stringify(paramData);

        //保存项目
        $.ajax({
            url:"/admin/advert/save",
            type:"POST",
            data:data2,
            // dataType:"json",
            contentType:"application/json;charset=utf-8",
            success:function (result) {
                // console.log("data=="+form_data)
                if(result.flag){
                    layer.msg(result.message);
                    //清空内容
                    // $("#form").reset();
                    document.getElementById("form").reset();
                    layui.form.render();
                }else{
                    layer.msg(result.message);
                }
            },
            error:function () {
                layer.msg("系统错误！");
            }
        });

    });

});


// 设置最小可选的日期
function minDate(){
    var now = new Date();
    return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + (now.getDate()+1);
}

